*Replication Code: Did the Budapest Convention Deter Cybercrime? An Event Study Using a Panel of Countries
*Authors: Humza Khan and Pengfei Zhang
*Data: Ransomware (Comparitech)

* Install programs
ssc install reghdfe
ssc install event_plot
ssc install did_imputation
ssc install csdid
ssc install drdid
ssc install ivreg2
ssc install avar
ssc install eventstudyinteract
ssc install did_multiplegt

* Import data 

* Converts strings to numeric
destring, replace

** Reshape dataset 

* Unique numeric identifier for each country (panel ID)
gen country_num = _n 

*Reshape the data from wide to long format
reshape long attack GDP Internet Education, i(country_num) j(year)

* Dataset as a time series dataset
tsset country_num year    

* Create the post-treatment dummy and the the timeToTreat dummy
gen timeToTreat = year - Eventtime 
gen treatment = cond(year >= Eventtime,1,0)

*1 OLS 
regress attack treatment

*2 country fixed effect
reghdfe attack treatment, absorb(country_num)

*3 Two-Way Fixed Effects 
reghdfe attack treatment, absorb(country_num year) cluster(country_num)

*4 Two-Way Fixed Effects with Controls
reghdfe attack treatment GDP Internet Education, absorb(country_num year) 

* Create the event time dummies
drop if timeToTreat < -17
drop if timeToTreat > 17

* event windows 
forvalues l = 0/16 {  
		gen L`l'event = timeToTreat == `l'
	} 

	forvalues l = 1/4 { 
		gen F`l'event = timeToTreat ==-`l' 
} 

drop F1event //counterfactual

*Dynamic Regression  
reghdfe attack F*event L*event, absorb(country_num year) cluster(country_num)
estimates store ols 

* check event window
tab timeToTreat

* Event plot
event_plot ols, ///
	stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
	graph_opt(ytitle("Estimated Change in Ransomware Attacks") ylabel(, labsize(small)) xlabel(-4(1)16, labsize(small)) xtitle("Years Before and After Ratification") ///
		legend(off)	/// 
		xline(-1, lcolor(gs13) lpattern(dash)) yline(0, lcolor(gs13)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
	) ///
	lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black)) 




